package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.Introducer;
import com.rongke.service.IntroducerService;
import com.rongke.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @IntroducerController
 * @介绍人Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/introducer")
@Transactional
@CrossOrigin
public class IntroducerController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private IntroducerService introducerService;

    /**
     * @添加介绍人
     * @param introducer
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addIntroducer(@RequestBody Introducer introducer){
        log.debug("添加介绍人");
        introducerService.insert(introducer);
        return JsonResp.ok(introducer);
    }

    /**
     * @修改介绍人
     * @param introducer
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateIntroducer(@RequestBody Introducer introducer){
        log.debug("修改介绍人");
        introducerService.updateById(introducer);
        return JsonResp.ok(introducer);
    }


    /**
     * @根据id删除介绍人
     * @param id 介绍人id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/delOne", method = RequestMethod.GET)
    public JsonResp delOne(Long id){
        log.debug("修改介绍人");
        introducerService.deleteById(id);
        return JsonResp.ok("","删除成功");
    }


    /**
     * @根据id查找介绍人
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectIntroducer(Long id){
        log.debug("查找介绍人");
        Introducer introducer = introducerService.selectById(id);
        return JsonResp.ok(introducer);
    }

    /**
     * @根据条件查找介绍人
     * @param introducer
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectByCondition", method = RequestMethod.POST)
    public JsonResp selectIntroducerByCondition(@RequestBody Introducer introducer){
        log.debug("根据条件查找介绍人");
        if(introducer==null)
            return JsonResp.toFail("参数不能为空");
        List<Introducer> introducerList=introducerService.selectByCondition(introducer);
        return JsonResp.ok(introducerList);
    }

    /**
     * @获取介绍人列表
     * @param phone 介绍人电话
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/findIntroducerlist", method = RequestMethod.GET)
    public JsonResp findIntroducerlist(String phone){
        log.debug("查找介绍人");
        List<Introducer> introducerList=null;
        if(StringUtil.isNotEmpty(phone)){
            EntityWrapper<Introducer> ew=new EntityWrapper<>();
            ew.eq("phone",phone);
            introducerList=introducerService.selectList(ew);
        }else {
            introducerList=introducerService.selectList(null);
        }
        return JsonResp.ok(introducerList);
    }


}
